package com.lcy.web.service.base;

import com.jfinal.core.Controller;
import com.lcy.web.service.render.JsonRender;
import com.lcy.web.service.render.MyRender;

/**
 * Created by loucunyin on 2017/1/11 0011.
 * 封装 service 的通用实现
 */
public abstract class BaseService implements Service{
    private MyError mError = MyError.success;
    private Controller mController;
    private Object mData;

    private MyRender mRender;

    public BaseService(Controller controller) {
        mController = controller;
        mRender = new JsonRender(this);
    }

    @Override
    public void handle() {
        initParams();

        if(!checkParams()){
            renderError();
            return;
        }

        if(process()){
            render();
        }else {
            renderError();
        }
    }


    @Override
    public void render() {
        mRender.render();
    }

    @Override
    public void renderError() {
        mRender.renderError();
    }

    public MyError getError() {
        return mError;
    }

    public Controller getController() {
        return mController;
    }

    public Object getData() {
        return mData;
    }

    public void setRender(MyRender render) {
        mRender = render;
    }
}
